package by.averianov.discounta.dataaccess.impl;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.SingularAttribute;

import org.hibernate.jpa.criteria.OrderImpl;
import org.springframework.stereotype.Repository;

import by.averianov.discounta.dataaccess.CardDao;
import by.averianov.discounta.datamodel.Card;
import by.averianov.discounta.datamodel.Card_;

@Repository
public class CardDaoImpl extends AbstractDaoImpl<Long, Card> implements CardDao {

	protected CardDaoImpl() {
		super(Card.class);
	}

	/*@Override
	public List<Card> getAllCard() {
		CriteriaBuilder cb = getEm().getCriteriaBuilder();

		CriteriaQuery<Card> q = cb.createQuery(Card.class);
		Root<Card> c = q.from(Card.class);

		q.select(c);

		TypedQuery<Card> query = getEm().createQuery(q);
		List<Card> results = query.getResultList();
		return results;
	}*/
	
	@Override
	public Card getCardByUser(Long userId){
		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<Card> criteria = cb.createQuery(Card.class);
        Root<Card> root = criteria.from(Card.class);

        criteria.select(root);
        criteria.where(cb.equal(root.get(Card_.userProfile), userId));

        TypedQuery<Card> query = getEm().createQuery(criteria);
        Card results = query.getSingleResult();
        return results;
	}
	
	@Override
	public List<Card> getAllCard(SingularAttribute<Card, ?> attr, boolean ascending, int startRecord, int pageSize) {
		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<Card> criteria = cb.createQuery(Card.class);
		Root<Card> root = criteria.from(Card.class);

		criteria.select(root);
		criteria.orderBy(new OrderImpl(root.get(attr), ascending));
		
		TypedQuery<Card> query = getEm().createQuery(criteria);
		query.setFirstResult(startRecord);
		query.setMaxResults(pageSize);
		List<Card> results = query.getResultList();
		return results;
	}
	
	@Override
	public List<Card> getAllCardByUser(Long userId) {
		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<Card> criteria = cb.createQuery(Card.class);
		Root<Card> root = criteria.from(Card.class);

		criteria.select(root);
		criteria.where(cb.equal(root.get(Card_.userProfile), userId));
		
		TypedQuery<Card> query = getEm().createQuery(criteria);
		List<Card> results = query.getResultList();
		return results;
	}

	@Override
	public List<Card> getAllCardByUser(SingularAttribute<Card, ?> attr, boolean ascending, int startRecord, int pageSize, Long userId) {
		CriteriaBuilder cb = getEm().getCriteriaBuilder();

		CriteriaQuery<Card> criteria = cb.createQuery(Card.class);
		Root<Card> root = criteria.from(Card.class);

		criteria.select(root);
		criteria.where(cb.equal(root.get(Card_.userProfile), userId));
		criteria.orderBy(new OrderImpl(root.get(attr), ascending));
		TypedQuery<Card> query = getEm().createQuery(criteria);
		query.setFirstResult(startRecord);
		query.setMaxResults(pageSize);

		List<Card> results = query.getResultList();
		return results;
	}

}
